/**
 * 手机端下拉组件
 * Created by KY on 2017/8/15
 **/
define(['ldfw/core','ldfw/ldlibs/dict','jquery-weui'], function (LDFW,dict) {
    return LDFW.Component.extend({
        initialize: function () {
            const _this = this
            this.$el.attr('readonly','readonly')
            // this.$el.hide()
            // let $select = $('<span class="select-select"><span class="select-select__rendered"></span></span>')
            //     $select.insertAfter(this.$el)
            let defaultValue = this.defaultValue = this.$el.attr('value')||this.$el.val(),
                omitValue = this.omitValue = this.$el.attr('ld-omit-value'),
                bindDict = this.$el.attr('ld-bind-dict'),
                bindData = this.$el.attr('ld-bind-data')
            // if(placeholder){
            //     $select.find('.select-select__rendered')
            //         .html('<span class="select-select__placeholder">'+placeholder+'</span>')
            // }
            // this.$select = $select
            if(bindDict){
                this.setItems(dict.getDictItems(bindDict))
                dict.on('change', function () {
                    _this.setItems(dict.getDictItems(bindDict))
                })
            }
            if(bindData){
                let model = this.parent.model
                this.setItems(model.get(bindData))
                model.on('change:'+bindData, function(changed) {
                    _this.setItems(changed)
                })
            }
        },

        setValue:function(item){
            // this.$select.find('.select-select__placeholder').remove()
            // this.$select.find('.select-select__rendered').text(item.title)
            if(_.isObject(item)){
                this.$el.val(item.title)
                this.$el.attr('value',item.title)
                this.$el.attr('data-values',item.value)
            }else{
                this.$el.val(item)
                this.$el.attr('value',item)
                this.$el.attr('data-values',item)
            }
        },
        getValue:function(){
            return this.$el.val()
        },
        setItems: function (data) {
            const _this = this
            let selectItems = [],
                selectOption = {},
                defaultValue = this.defaultValue,
                defaultValueIndex = 0
            try {
                if (defaultValue) {
                    defaultValue = JSON.parse(defaultValue)
                }
            } catch (e) {
                defaultValue = defaultValue
            }
            if(_.isArray(data)){
                _.each(data, function (title) {
                    if(!_.contains(_this.omitValue,title)){
                        selectItems.push(title)
                        if(defaultValue && defaultValue == title){
                            defaultValueIndex = selectItems.length-1
                        }
                    }
                })
            }else if(_.isObject(data)){
                _.each(data, function (title, value) {
                    if(typeof title === typeof ''){}
                    let item = {title: title, value: value}
                    if(!_.contains(_this.omitValue,value)){
                        selectItems.push(item)
                        if(defaultValue && defaultValue == value){
                            defaultValueIndex = selectItems.length-1
                        }
                    }
                })
            }
            if(defaultValue){
                this.setValue(selectItems[defaultValueIndex])
            }
            this.$el.select({
                items:selectItems,
                onOpen:function(select){
                    select.$input.blur()
                },
                onClose:function(select){
                    if(select.data.length){
                        _this.trigger('change',select.data)
                    }
                }
            })
        }
    })
})